package com.xfc.web.controller.school;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xfc.common.annotation.Log;
import com.xfc.common.core.controller.BaseController;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.core.page.TableDataInfo;
import com.xfc.common.enums.BusinessType;
import com.xfc.common.utils.StringUtils;
import com.xfc.common.utils.poi.ExcelUtil;
import com.xfc.school.domain.ScTeacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xfc.school.domain.ScStudent;
import com.xfc.school.service.IScStudentService;


/**
 * 学生信息Controller
 * 
 * @author lcj
 * @date 2021-12-05
 */
@Api(value = "student", tags = "学生信息管理接口")
@RestController
@RequestMapping("/school/student")
public class ScStudentController extends BaseController
{
    @Autowired
    private IScStudentService scStudentService;

    /**
     * 查询学生信息列表
     */
    @ApiOperation("查询学生信息列表")
    @PreAuthorize("@ss.hasPermi('school:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScStudent scStudent)
    {
        startPage();
        List<ScStudent> list = scStudentService.list(Wrappers.query(scStudent)
                .orderByDesc("create_time"));
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('school:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScStudent scStudent)
    {
        List<ScStudent> list = scStudentService.list(Wrappers.query(scStudent));
        ExcelUtil<ScStudent> util = new ExcelUtil<>(ScStudent.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 获取学生信息详细信息
     */
    @ApiOperation("获取学生信息详细信息")
    @PreAuthorize("@ss.hasPermi('school:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(scStudentService.getById(id));
    }

    /**
     * 新增学生信息
     */
    @ApiOperation("新增学生信息")
    @PreAuthorize("@ss.hasPermi('school:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScStudent scStudent)
    {
        //新增学生编号
        QueryWrapper<ScStudent> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("student_code");
        queryWrapper.last("limit 1");
        ScStudent student1=scStudentService.getOne(queryWrapper);
        if (Objects.nonNull(student1)){
            scStudent.setStudentCode(student1.getStudentCode()+1);
        }else {
            scStudent.setStudentCode(100);
        }
        scStudent.setCreateTime(new Date());

        return toAjax(scStudentService.save(scStudent));
    }

    /**
     * 修改学生信息
     */
    @ApiOperation("修改学生信息")
    @PreAuthorize("@ss.hasPermi('school:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScStudent scStudent)
    {
        scStudent.setUpdateTime(new Date());
        return toAjax(scStudentService.updateById(scStudent));
    }

    /**
     * 删除学生信息
     */
    @ApiOperation("删除学生信息")
    @PreAuthorize("@ss.hasPermi('school:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
		List<String> list=new ArrayList<>();
    	if (ids!=null&&ids.length>0) {
            for (String id : ids) {
                list.add(id);
            }
		}
        return toAjax(scStudentService.removeByIds(list));
    }

    /**
     * 根据学生姓名查询学生信息
     */
    @ApiOperation("根据学生姓名查询学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="studentName",value="学生姓名",dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('school:student:list')")
    @GetMapping("/getUserByName")
    public AjaxResult getUserByName(String studentName) {

        QueryWrapper<ScStudent> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(studentName)){
            queryWrapper.like("student_name",studentName);
        }
        queryWrapper.eq("state",0);
        queryWrapper.select("id","student_name","student_mobile");
        List<ScStudent> list = scStudentService.list(queryWrapper);
        return AjaxResult.success(list);
    }





}
